import DrawActionBase from "./DrawActionBase";
import {CVT} from "../utils/utils";
import DrawState from "./DrawState";
import GraphicType from "./GraphicType";
import {CesiumPolyline} from "./Graphic";

class DrawActionGraffiti extends DrawActionBase{
    constructor(props) {
        super(props);
        this._viewer = window.cesiumViewer;
        this._positions = [];
        this._entity = null;
        this._lastMousePos = null;
        this._idList = [];
    }

    type(){return GraphicType.GRAFFITI;}


    leftDown(e) {

        let pickedPosition=CVT.pixel2Cartesian(e.position,this._viewer);
        let self = this;
        // function _update() {
        //     return self._positions;
        // }
        self._viewer.scene.screenSpaceCameraController.enableRotate = false;
        self._viewer.scene.screenSpaceCameraController.enableTranslate = false;
        self._viewer.scene.screenSpaceCameraController.enableZoom = false;
        self._viewer.scene.screenSpaceCameraController.enableLook = false;
        self._viewer.scene.screenSpaceCameraController.enableTilt = false;

        // self._positions = [];
        //
        // self._positions.push(pickedPosition);
        // this._entity = new Cesium.Entity({
        //     id: this.generateId(),
        //     _name:this.generateName(),
        //     _type:GraphicType.GRAFFITI,
        //     polyline: {
        //         positions: new Cesium.CallbackProperty(_update, false),
        //         show: true,
        //         material: DrawState.lineColor,
        //         width: DrawState.lineWidth,
        //         clampToGround: DrawState.lineClampToGround
        //     }
        // });
        this._lastMousePos = e.position;
        // this.getDataSource().entities.add(this._entity);
        let options = {};
        options.positions = [];
        options.clampToGround = DrawState.lineClampToGround;
        options.material = DrawState.getLineMaterial();
        options.width = DrawState.lineWidth;
        const manager = new CesiumPolyline(this._viewer, options);
        manager.gvid = this.generateId();
        manager.gvname = this.generateName();
        manager.heightReference = DrawState.lineHeightReference;
        manager.addNode(pickedPosition);
        this.manager = manager;
    }

    leftUp(e) {
        let self = this;

        let pickedPosition=CVT.pixel2Cartesian(e.position,this._viewer);
        self.manager.addNode(pickedPosition);
        //self._positions.push(pickedPosition);
        self._lastMousePos = null;
        //self._entity.polyline.positions = self._positions;
        //self._positions = [];
        //self._entity = null;
        this._idList.push(self.manager.gvid);
        if(this._callback){
            this._callback(self.manager);
        }
    }

    mouseMove(e) {
        if(!e.endPosition) return;
        if(!this._lastMousePos) return;
        if(Cesium.Cartesian2.distanceSquared(this._lastMousePos,e.endPosition) < 5) return;
        this._lastMousePos.x = e.endPosition.x;
        this._lastMousePos.y = e.endPosition.y;
        let pickedPosition=CVT.pixel2Cartesian(e.endPosition,this._viewer);
        this.manager.addNode(pickedPosition);
        // this._positions.push(pickedPosition);
        // console.log(pickedPosition);
    }

    undo() {
        if(this._idList.length < 1) return;
         //= this._idList[this._idList.length -1];
        let id = this._idList.pop();
        return id;
    }

    stop() {
        this._viewer.scene.screenSpaceCameraController.enableRotate = true;
        this._viewer.scene.screenSpaceCameraController.enableTranslate = true;
        this._viewer.scene.screenSpaceCameraController.enableZoom = true;
        this._viewer.scene.screenSpaceCameraController.enableLook = true;
        this._viewer.scene.screenSpaceCameraController.enableTilt = true;
    }

}

export default DrawActionGraffiti;